# -*- coding:utf-8 -*-

# author: devops_yj@163.com

"""
import threading
import time

def add(x,y):
    print(x + y)

t = threading.Timer(3,add,args={4,5})
t.cancel()
t.start()

time.sleep(2)
# t.cancel()
"""

"""
import threading,time,logging

FORMAT = '%(asctime)s %(threadName)s %(thread)d %(message)s'
logging.basicConfig(format=FORMAT,level=logging.INFO)

def worker():
    logging.info('in worker')
    time.sleep(2)

t = threading.Timer(5,worker)
t.setName('w1')

t.start()  # 启动线程
print(threading.enumerate())
t.cancel() # 取消,可以注释这一句看看如何定时执行.
time.sleep(1)
print(threading.enumerate())
"""
'''
如果线程中worker函数已经开始执行,cancel就没有任何效果了.
总结:
    Timer是线程Thread的子类,就是线程类,具有线程的能力和特征.
    它的实例是能够延时执行目标函数的线程,在真正执行目标函数之前,都可以cancel它.
'''

"""
# 提前cancel
import threading,logging,time

FORMAT = '%(asctime)s %(threadName)s %(thread)d %(message)s'
logging.basicConfig(format=FORMAT,level=logging.INFO)

def worker():
    logging.info('in worker')
    time.sleep(2)

t = threading.Timer(5,worker)
t.setName('w1')
t.cancel() # 提前取消
t.start()  # 启动线程
print(threading.enumerate())
time.sleep(8)
print(threading.enumerate())
"""

"""
import threading,time,logging
logging.basicConfig(level=logging.INFO)

def add(x,y):
    logging.info("{} {}".format(threading.enumerate(),x+y))

t = threading.Timer(1,add,args=(4,5))
t.start()
"""


# 举例,默认级别
"""
import logging

FORMAT = '%(asctime)-15s\tThread info: %(thread)d %(threadName)s %(message)s'
logging.basicConfig(format=FORMAT)    

logging.info('I am {}'.format(20)) # info不显示
logging.warning('I am {}'.format(20)) # warning默认级别.
"""


# 构建消息
"""
import logging

FORMAT = '%(asctime)-15s\tThread info: %(thread)d %(threadName)s %(message)s'

logging.basicConfig(format=FORMAT,level=logging.INFO)

logging.info('I am {}'.format(20)) # 单一字符串
logging.info('I am %d %s',20,'years old.') # C风格
# 基本使用方法,使用的是info,正常运行信息的输出.
"""



# 日志级别和格式字符串扩展的例子
"""
import logging
FORMAT = '%(asctime)-15s\tThread info: %(thread)d %(threadName)s %(message)s %(school)s'

logging.basicConfig(format=FORMAT,level=logging.WARNING)

d = {'school':'yuan'}
logging.info('I am %s %s',20,'years old.',extra=d)
logging.warning('I am %s %s',20,'years old.',extra=d)
"""


# 修改日期格式
"""
import logging

logging.basicConfig(format='%(asctime)s %(message)s',datefmt='%Y%m%d %I:%M:%S')
logging.warning('this event was logged.')
"""


# 输出到文件:
"""
import logging

logging.basicConfig(format='%(asctime)s %(message)s',filename='./two.log')

for _ in range(5):
    logging.warning('this event was logged')

log = logging.getLogger()
print(log.name)
print(log,type(log))
"""



"""
import threading,time,logging

FORMAT = "%(asctime)s %(thread)d %(message)s %(schoolname)s"
logging.basicConfig(level=logging.INFO,format=FORMAT,datefmt="%Y-%m-%d-%H:%M:%S")

d = {'test': 'abc'}

def add(x,y):
    log = logging.getLogger('')
    print(log.name)
    print(log,type(log))

    log.debug("{} {}".format(threading.enumerate(),x+y))
    # log.info("%s %s",x,y)

t = threading.Timer(1,add,args=(4,5))
t.start()
"""



# Logger类
"""
import logging

# 层级关系
FORMAT = '%(asctime)-15s\tThread info: %(thread)d %(threadName)s %(message)s'
# logging.basicConfig(format=FORMAT,level=logging.INFO)
logging.basicConfig(level=logging.WARNING,format=FORMAT,datefmt="%Y-%m-%d-%H:%M:%S")
"""
"""
root = logging.getLogger()  # root根logger
print(root,id(root))
root.info('my root')

loga = logging.getLogger('a')
print(loga,id(loga),id(loga.parent))
loga.warning('my logs')

loga = logging.getLogger('a.b')
print(loga,id(loga),id(loga.parent))
loga.warning('my a.b')


print(root.name,type(root),root.parent,id(root))

logger = logging.getLogger(__name__)  # 模块级的logger
print(logger.name,type(logger),id(logger),id(logger.parent))

logger1 = logging.getLogger(__name__+".ok")
"""

"""
root = logging.getLogger()
print(root,id(root))
root.warning('my root')

loga = logging.getLogger(__name__)
print(loga,id(loga),id(loga.parent))
loga.warning('my loga')

loga = logging.getLogger("{}.{}".format(__name__,'echo'))
print(loga,id(loga),id(loga.parent))
loga.warning('my a.b')
"""


#  级别设置
import logging

FORMAT = '%(asctime)-15s\tThread info: %(thread)d %(threadName)s %(message)s'
logging.basicConfig(format=FORMAT,level=logging.INFO)

logger = logging.getLogger(__name__)
print(logger.name,type(logger))
print(logger.getEffectiveLevel())  # INFO 20
logger.info('hello1')
logger.setLevel(28)
print(logger.getEffectiveLevel()) # INFO 28
logger.info('hello2')
logger.warning('warning')


